function ham_replace(div,req)
{
	if(req.readyState == 4)
	{
		if(req.status == 200)
			div.innerHTML = req.responseText;
		else
			div.innerHTML = "ERROR";
	}
}

function ham_get_div(tag)
{
	while(tag.tagName != "DIV")
		tag = tag.parentNode;
	return tag;
}

function ham_go(div,url)
{
	var request;
	if (window.XMLHttpRequest)     // Object of the current windows
	{ 
		request = new XMLHttpRequest();     // Firefox, Safari, ...
	} 
	else if (window.ActiveXObject)   // ActiveX version
	{
		request = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer 
	}
	request.onload = function()
	{
		ham_replace(div,request);
	}
	request.open("GET",url,true);
	request.send(null);
}

function ham_a(a,url)
{
	return ham_go(ham_get_div(a),url);
}

hamSelectors = new Array();
hamConnections = new Array();

function HamSelector(target_div,target_input,title)
{
	this.div = target_div;
	this.input = target_input;
	this.index = hamSelectors.length;
	if(target_input.value == "")
		this.selected = new Array();
	else
		this.selected = target_input.value.split(',');
	this.title = title;
	hamSelectors.push(this);
	this.update = function()
	{
		str = "<table><thead>\n<tr><td>"+this.title+"</td></tr></thead>\n<tbody>\n";
		for(var i=0;i<this.selected.length;i++)
			str += '<tr><td><a href="javascript:null" onclick="ham_unselect('+this.index+','+i+')">'+this.selected[i]+'</a></td></tr>\n';
		str += '</tbody>\n</table>';
		this.div.innerHTML = str;
		this.input.value = this.selected.join(',');
	};
	this.find = function(str)
	{
		for(var i=0;i<this.selected.length;i++)
		{
			if(this.selected[i]== str)
				return i;
		}
		return this.selected.length;
	};
	this.select = function(str)
	{
		if(this.find(str) == this.selected.length)
			this.selected.push(str);
		this.update();
	};
	this.unselect = function(where)
	{
		if(where != this.selected.length)
		{
			this.selected[where] = this.selected[this.selected.length - 1]; 
			this.selected.pop();
			this.update();
		}
	};
	this.update();
}

function ham_connect(sourceDivId,targetDivId,inputId,title)
{
	h = new HamSelector(document.getElementById(targetDivId),document.getElementById(inputId),title);
	hamConnections["hamc_"+sourceDivId] = h;
}
function ham_real_select(sourceDivId,what)
{
	hamConnections["hamc_"+sourceDivId].select(what);
}
function ham_select(tag,what)
{
	ham_real_select(ham_get_div(tag).id,what);
}
function ham_unselect(index,what)
{
	hamSelectors[index].unselect(what);
}
